#pragma once
#include "afxwin.h"

#include "atlimage.h"

#include "ScreenRayPicker.h"

#include <vector>

#include "OglViewInterfaces.h"

class CDrawableWidget :
	public CWnd
{
	
	std::vector<ICustomDraw *> m_vecCustomDraw;
	ICustomOnMouse * m_pCustomOnMouse;
		// CDC * m_ghDC;
		RECT m_rect;
		bool m_bOglInitialized;
		PAINTSTRUCT m_ps;
		BOOL SetupPixelFormat();
		
		UINT m_nMouseFlags;
		CPoint m_pointMouseAsInts;
		double m_arrLfMousePos[2];

		double m_lfAspect;

		bool m_bPerspective;

		bool m_bMouseStarted;


		bool m_bPixelsSetUp;

		
		void DrawGlScene(void);
		HGLRC InitOglStuff();

		CDrawableWidget * m_pLinkedWidget;

		CDC * m_pCDC;

		CDC * SafeGrabDC();
		void SafeReleaseDC();

		double m_lfNear, m_lfFar;

		ScreenRayPicker m_rayPicker;
		bool m_bRayPickerValid;

		viewInfo * m_pViewInfo;

	public:

		void SetNearFar(double lfNear, double lfFar) { m_lfNear = lfNear; m_lfFar = lfFar; }
		double GetNear() const { return m_lfNear; }
		double GetFar() const { return m_lfFar; }

		void MakeRayPicker() { m_bRayPickerValid = true;}

		CDrawableWidget();
		
		void SetPerspective(bool bIsPerspective) { m_bPerspective = bIsPerspective; }
		// virtual ~CarSimDrawButton() {}
		void DrawScreen();

		bool SetRayPickerVals(ScreenRayPicker & SetMe);

		const ScreenRayPicker & GetRayPicker() const { return m_rayPicker; }

		void SetCustomOnMouse(ICustomOnMouse * pOnMouse) { m_pCustomOnMouse = pOnMouse; }

		void SetExternalViewInfo(viewInfo * pViewInfo) { m_pViewInfo = pViewInfo; } // nasty engineering

		void AddCustomDraw(ICustomDraw * pCustomDraw) { m_vecCustomDraw.push_back(pCustomDraw); }

		int PopCustomDraw() { m_vecCustomDraw.pop_back(); return m_vecCustomDraw.size(); }

		bool GrabScreen(CImage & imgGrabTo);

		CImage * GetBlankImage(double lfSizeScale, bool bHasAlpha);

		bool SaveScreen(const CString & strFileName);

		void LinkTo(CDrawableWidget & widgetToLink)
		{
			m_pLinkedWidget = &widgetToLink;
			widgetToLink.m_pLinkedWidget = this;
		}

		CDrawableWidget * PopLink()
		{
			CDrawableWidget *pResult = m_pLinkedWidget;
			if(pResult)
			{
				pResult->m_pLinkedWidget = NULL;
			}
			return pResult;
		}
		
	protected:
		afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
		afx_msg void OnPaint();
		afx_msg void OnMouseMove(UINT nFlags, CPoint point);
		DECLARE_MESSAGE_MAP()
};
